<template>
  <div class="tims-navbar">
    <div class="tims-navbar-menus">
      <img class="tims-navbar-logo" src="@/assets/logo.png" />
      <el-menu :default-active="activeMenu1" router class="tims-navbar-menu" mode="horizontal">
        <el-menu-item index="/tims/home/index/classroomList">首页</el-menu-item>
        <el-menu-item index="/tims/home/announcement">公告</el-menu-item>
        <el-menu-item style="width: 100px;" index="/tims/home/classroom">我的班级</el-menu-item>
        <el-menu-item style="width: 100px;" index="/tims/home/course">我的精品课</el-menu-item>
        <el-menu-item style="width: 100px;" index="/tims/home/schedule">我的课程表</el-menu-item>
      </el-menu>
    </div>
    <div class="tims-navbar-students">
      <img v-if="isLogin" class="tims-message" @click="goMessage" src="@/assets/message/message.png" />
      <el-dropdown v-if="isLogin">
        <div class="tims-navbar-name">
          <img class="tims-head-image" v-if="isLogin" :src="studentInfo.headImage" />
          <span>{{ studentInfo.nickName }}</span>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="goStudentDetail">
              <div class="tims-navabar-item">
                <span>个人中心</span>
              </div>
            </el-dropdown-item>
            <el-dropdown-item>
              <div class="tims-navabar-item">
                <span @click="handleLogout">退出登录</span>
              </div>
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
      <span class="tims-navbar-login-btn" v-if="!isLogin" @click="goLogin">登录</span>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, watch } from 'vue';
import router from '@/router';
import { getToken, removeToken } from '@/utils/cookie';
import { logoutService, getStudentInfoService } from '@/apis/student';
import { useRoute } from 'vue-router';

const route = useRoute();
const activeMenu1 = ref(route.meta.activeMenu1); // 反应当前路由
const activeMenu2 = ref(route.meta.activeMenu2); // 反应当前路由

watch(route, (to) => {
  activeMenu1.value = to.meta.activeMenu1; // 监听路由变化，更新activeMenu
  console.log(to.meta.activeMenu1)
  activeMenu2.value = to.meta.activeMenu2; // 监听路由变化，更新activeMenu
});
const isLogin = ref(false)
const studentInfo = reactive({
  nickName: '',
  headImage: ''
})

async function checkLogin() {
  if (getToken()) {
    //  后端是需要提供一个接口完成，这两件事情的
    //  1. 判断当前token是否过期（判断当前用户是否还处于登录状态）
    //  2. 将当前用户的头像、昵称返回
    const studentInfoRes = await getStudentInfoService()
    Object.assign(studentInfo, studentInfoRes.data)
    isLogin.value = true
  }
}
checkLogin()

function goLogin() {
  router.push('/tims/login')
}

function goStudentDetail() {
  router.push('/tims/home/student/detail')
}

// function goMessage() {
//   router.push('/tims/home/student/message')
// }

async function handleLogout() {
  await ElMessageBox.confirm(
    '确认退出',
    '温馨提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '退出',
      type: 'warning',
    }
  )
  await logoutService()
  removeToken()
  isLogin.value = false
  router.push('/tims/login')
}


</script>

<style lang="scss" scoped>
.tims-navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  box-sizing: border-box;

  max-width: 1520px;
  margin: 0 auto;

  .tims-navbar-menus {
    display: flex;
    align-items: center;
    height: 50px;

    .el-menu-item {
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 20px;
      color: #222222;
      line-height: 28px;
      text-align: center;
      width: 42px;
      text-align: left;
      margin-right: 25px;
    }
  }

  .tims-navbar-logo {
    width: 38px;
    height: 38px;
    background: #32C5FF;
    border-radius: 8px;
    cursor: pointer;
    object-fit: contain;
    margin-right: 59px;
  }

  .tims-navbar-menu {
    // margin-left: 18px;
    width: 600px;
    border: none;

    .el-menu-item {
      font-size: 16px;
      font-weight: 500;
      background-color: transparent !important;
      transition: none;
      border: none;
      line-height: 60px;
    }
  }

  .tims-navbar-students {
    display: flex;
    align-items: center;
  }

  .tims-navbar-login-btn {
    line-height: 60px;
    display: inline-block;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 18px;
    color: #222222;
    text-align: center;
    cursor: pointer;

    .line {
      display: inline-block;
      width: 25px;
    }
  }

  .tims-message {
    cursor: pointer;
    margin-top: 15px;
  }

  .tims-head-image {
    width: 30px;
    height: 30px;
    border-radius: 30px;
    margin-right: 10px;
  }

  .tims-navbar-name {
    cursor: pointer;
    margin-top: 15px;
    font-weight: 400;
    color: #000;
    margin-left: 15px;
    font-size: 20px;
    width: 100px;
    display: flex;
    align-items: center;
  }

  .tims-navabar-item {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 32px;
  }
}
</style>